/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: DrivingLightsFrame.java Package:
 * tc.de.krawallmann.robobridge.visualization Version: 1.0 Author(s):
 * KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge.visualization;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JPanel;

import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.implementation.DrivingLights;
import eu.hansolo.steelseries.tools.LedColor;

/**
 * The Class DrivingLightsFrame.<br/>
 * Partially auto-generated Javadoc!
 */
public class DrivingLightsFrame extends AbstractRoboViewDisplay {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2734972913873127353L;
	long lastTime = System.currentTimeMillis();
	private final FixedLed bfl;
	private final FixedLed bfr;
	private final FixedLed bl;
	private final FixedLed br;
	private final FixedLed brl;
	private final FixedLed brr;
	private final DrivingLights d;
	private final FixedLed fl;
	private final FixedLed fr;
	private final FixedLed rl;

	private final FixedLed rr;

	/**
	 * Instantiates a new driving lights frame.
	 * 
	 * @param d
	 */
	public DrivingLightsFrame(final DrivingLights d) {
		super("DrivingLights");
		this.d = d;
		setLayout(new GridLayout(1, 2));
		final JPanel left = new JPanel(new GridLayout(2, 3));
		bfl = new FixedLed(666);
		bfl.setLedColor(LedColor.ORANGE);
		left.add(bfl);
		fl = new FixedLed(666);
		fl.setLedColor(LedColor.CUSTOM);
		fl.setCustomLedColor(Color.WHITE);
		left.add(fl);
		left.add(new JPanel());
		brl = new FixedLed(666);
		brl.setLedColor(LedColor.ORANGE);
		left.add(brl);
		bl = new FixedLed(666);
		bl.setLedColor(LedColor.RED);
		left.add(bl);
		rl = new FixedLed(666);
		rl.setLedColor(LedColor.CUSTOM);
		rl.setCustomLedColor(Color.WHITE);
		left.add(rl);

		left.setToolTipText("<html>The status of the left driving lights.<br/>This is just a simulation, the status of the real lights<br/>may vary by some milliseconds!");

		final JPanel right = new JPanel(new GridLayout(2, 3));
		right.add(new JPanel());
		fr = new FixedLed(666);
		fr.setLedColor(LedColor.CUSTOM);
		fr.setCustomLedColor(Color.WHITE);
		right.add(fr);
		bfr = new FixedLed(666);
		bfr.setLedColor(LedColor.ORANGE);
		right.add(bfr);

		rr = new FixedLed(666);
		rr.setLedColor(LedColor.CUSTOM);
		rr.setCustomLedColor(Color.WHITE);
		right.add(rr);
		br = new FixedLed(666);
		br.setLedColor(LedColor.RED);
		right.add(br);
		brr = new FixedLed(666);
		brr.setLedColor(LedColor.ORANGE);
		right.add(brr);

		if (d.isBlinkingLeft()) {
			bfl.setLedBlinking(true);
			brl.setLedBlinking(true);
			bfr.setLedBlinking(false);
			brr.setLedBlinking(false);

		} else if (d.isBlinkingRight()) {
			bfl.setLedBlinking(false);
			brl.setLedBlinking(false);
			bfr.setLedBlinking(true);
			brr.setLedBlinking(true);
		} else if (d.isWarnBlinking()) {
			bfl.setLedBlinking(true);
			brl.setLedBlinking(true);
			bfr.setLedBlinking(true);
			brr.setLedBlinking(true);
		} 
		if (d.isHeadlightsOn()) {
			fl.setLedOn(true);
			fr.setLedOn(true);
			bl.setLedOn(true);
			br.setLedOn(true);
		} 
		if (d.isReverseLightOn()) {
			rl.setLedOn(true);
			rr.setLedOn(true);
		}

		right.setToolTipText("<html>The status of the right driving lights.<br/>This is just a simulation, the status of the real lights<br/>may vary by some milliseconds!");
		add(left);
		add(right);
		setSize(300, 300);
		setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * tc.de.krawallmann.robobridge.devices.SensorListener#onSensorValueChanged
	 * (tc.de.krawallmann.robobridge.devices.AbstractDevice, java.lang.String,
	 * java.lang.Object, java.lang.Object)
	 */
	@Override
	public void onSensorValueChanged(final AbstractDevice sensor, final String propertyName,
			final Object oldValue, final Object newValue) {
		if (sensor.equals(d)) {
			if (propertyName.equals(DrivingLights.BLINKING_LEFT)) {
				if ((boolean) newValue) {
					bfl.setLedBlinking(true);
					brl.setLedBlinking(true);
					bfr.setLedBlinking(false);
					brr.setLedBlinking(false);
				} else {
					bfl.setLedBlinking(false);
					brl.setLedBlinking(false);
					bfr.setLedBlinking(false);
					brr.setLedBlinking(false);
				}
			} else if (propertyName.equals(DrivingLights.BLINKING_RIGHT)) {
				if ((boolean) newValue) {
					bfl.setLedBlinking(false);
					brl.setLedBlinking(false);
					bfr.setLedBlinking(true);
					brr.setLedBlinking(true);
				} else {
					bfl.setLedBlinking(false);
					brl.setLedBlinking(false);
					bfr.setLedBlinking(false);
					brr.setLedBlinking(false);
				}
			} else if (propertyName.equals(DrivingLights.WARN_BLINKING)) {
				if ((boolean) newValue) {
					bfl.setLedBlinking(false);
					brl.setLedBlinking(false);
					bfr.setLedBlinking(false);
					brr.setLedBlinking(false);
					bfl.setLedBlinking(true);
					brl.setLedBlinking(true);
					bfr.setLedBlinking(true);
					brr.setLedBlinking(true);
				} else {
					bfl.setLedBlinking(false);
					brl.setLedBlinking(false);
					bfr.setLedBlinking(false);
					brr.setLedBlinking(false);
				}
			} else if (propertyName.equals(DrivingLights.HEADLIGHTS)) {
				if ((boolean) newValue) {
					fl.setLedOn(true);
					fr.setLedOn(true);
					bl.setLedOn(true);
					br.setLedOn(true);
				} else {
					fl.setLedOn(false);
					fr.setLedOn(false);
					bl.setLedOn(false);
					br.setLedOn(false);
				}
			} else if (propertyName.equals(DrivingLights.REVERSELIGHT)) {
				if ((boolean) newValue) {
					rl.setLedOn(true);
					rr.setLedOn(true);
				} else {
					rl.setLedOn(false);
					rr.setLedOn(false);
				}
			}
		}
	}

}
